import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path,{resolve} from 'node:path'
import fastGlob from 'fast-glob';
import dts from "vite-plugin-dts";

const list = fastGlob.sync('./src/ui/*/index.{tsx,ts,js,jsx}', { absolute: true, cwd: process.cwd() })
const entry = list.reduce((pre,next) => {
  const item = path.parse(next)
  const pathList = item?.dir.split('/')
  const name = pathList[pathList.length - 1]
  pre[name]=next
  return pre
},{});

export default defineConfig({
  plugins: [dts(),react()],
  build: {
    lib: {
      entry: {...entry,index:'./src/ui/index.ts'},
      formats:['es']
    },
    rollupOptions: {
      // 确保外部化处理那些你不想打包进库的依赖
      // (source: string, importer: string | undefined, isResolved: boolean) => { 
      //   return ['antd/es/button','antd/es/form','antd/es/input','antd','react','react-dom','react/jsx-runtime'].includes(source)
      // },
      external: ['react', 'react-dom', 'antd', 'react/jsx-runtime'],
      output: {
        entryFileNames(chunkInfo) {
          if(chunkInfo.name==='index'){
             return 'index.js'
          }
           return `${chunkInfo.name}/index.js`
        },
      }
    }       
  }
})

